package com.xunshangwang.pinhaohuo.ui.fragments.address;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.xunshangwang.pinhaohuo.R;
import com.xunshangwang.pinhaohuo.adapters.AddressNameAdapter;
import com.xunshangwang.pinhaohuo.ui.activities.address.SelectAddressActivity;

import java.util.List;

import tool.library.adapter.BaseRecyclerAdapter;

/**
 * Created by xzp on 16/2/16.
 */
public class CityFragment extends BaseAddressFragment{
    public static final CityFragment newInstance(int position){
        Bundle bundle=new Bundle();
        bundle.putInt("provincePos", position);
        CityFragment fragment=new CityFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
    private int mProvincePos;
    private CityAdapter mAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getArguments()!=null){
            mProvincePos=getArguments().getInt("provincePos");
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        SelectAddressActivity activity= (SelectAddressActivity) getActivity();
        mAdapter=new CityAdapter(getContext(),activity.getProvinceList().get(mProvincePos).getCityList());
        mAdapter.setOnItemClickListener(this);
        mRecycler.setAdapter(mAdapter);
    }

    @Override
    public void onItemClick(BaseRecyclerAdapter adapter, View item, int position) {
        SelectAddressActivity activity= (SelectAddressActivity) getActivity();
        FragmentTransaction transaction=activity.getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container,DistrictFragment.newInstance(mProvincePos,position),"district");
        transaction.addToBackStack(null);
        transaction.commitAllowingStateLoss();
        activity.getTitleText().setText(R.string.select_district);
    }
    private class CityAdapter extends AddressNameAdapter<SelectAddressActivity.City> {
        public CityAdapter(Context context, List list) {
            super(context, list);
        }

        @Override
        public void onBindHolder(RecyclerView.ViewHolder holder, int position) {
            NameHolder nameHolder= (NameHolder) holder;
            nameHolder.nameText.setText(getList().get(position).getName());
        }
    }
}
